home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PACKET / CBBS60SO.ZIP / MBRESTM.C < prev    next >
Text File  |  1989-02-19  |  8KB  |  399 lines

  1. /*
  2.  *  MBRESTM - 1/18/88
  3.  *
  4.  *  Copyright (C) 1988, 1989
  5.  *  By the CBBS Group.
  6.  *
  7.  *  This program reads the individual message text files and
  8.  *  produces MAIL.DAT from the information. It is intended to
  9.  *  be used after a mail file crash.
  10.  *
  11.  *  It is expected that this program will be run from the
  12.  *  directory containing the message files. The new MAIL.DAT
  13.  *  file will also be found in this directory. It will have to
  14.  *  copied the the proper bbs directory replacing the corrupted
  15.  *  original mail file.
  16.  *
  17.  */
  18.  
  19. #include "mb.h"
  20. #include "dos.h"
  21.  
  22. MAIL_HDR *mfhs;
  23. MSG_HDR *tmmhs;
  24. byte flds;
  25. char *fld[maxflds];
  26. char *cmd;
  27. char *line;
  28. char *nullstr = "";
  29.  
  30. char pp[44], name[500][13], msgfile[10], badfile[10];
  31. int count = 0;
  32. int cmp();
  33.  
  34. main()
  35. {
  36.   int x, c, y;
  37.   int xx = 1;
  38.   int recno = 1;
  39.   int msgfl, mail;
  40.   char *t;
  41.  
  42.   mfhs    =  (MAIL_HDR *) malloc(sizeof(MAIL_HDR));
  43.   tmmhs   =  (MSG_HDR *)  malloc(sizeof(MSG_HDR));
  44.   cmd     =  (char *) malloc(linelen);
  45.   line    =  (char *) malloc(linelen);
  46.  
  47.   if((mail = open("MAIL.DAT", O_CREAT | O_RDWR | O_BINARY, pmode)) < 0)
  48.     { puts("MAIL.DAT cannot be opened - process aborted!\n");
  49.       exit (1);
  50.     }
  51.  
  52.   dir_list();
  53.  
  54.   while( xx <= count)
  55.   {
  56.     y = true;
  57.     sprintf (msgfile, "%s",name[xx-1]);
  58.     printf("%s ", msgfile);
  59.     if((msgfl = open( msgfile, O_RDWR | O_BINARY)) < 0)
  60.       {
  61.         printf(" cannot be opened - bypassing this file\n");
  62.         xx++;
  63.         continue;
  64.       }
  65.  
  66.     read_rec(msgfl, 0, (char *) line);
  67.     if(!((line[253] is '\0') and (line[254] is '\r') and (line[255] is '\n')))
  68.       {
  69.         printf(" seems to be corrupted - bypassing this file\n");
  70.         close(msgfl);
  71.         sprintf(badfile, "%s.BAD", msgfile);
  72.         rename(msgfile, badfile);
  73.         xx++;
  74.         continue;
  75.       }
  76.  
  77.  
  78.     fill(tmmhs, '\0', 256);
  79.     tmmhs->rn       = xx;
  80.     tmmhs->read     = atoi( &line[61]);
  81.     tmmhs->number   = atoi( &line[0]);
  82.     tmmhs->size     = atoi( &line[9]);
  83.     tmmhs->type     = line[6];
  84.     switch (line[7])
  85.       {
  86.       case 'O': tmmhs->stat = m_stale; break;
  87.       case 'F': tmmhs->stat = m_fwd; break;
  88.       case 'H': tmmhs->stat = m_hold; break;
  89.       case 'Y': tmmhs->stat = m_read; break;
  90.       case 'K': tmmhs->stat = m_kill; break;
  91.       default:  tmmhs->stat = '\0';
  92.       }
  93.  
  94.     strncpy(tmmhs->to, &line[15], ln_call);
  95.     strncpy(tmmhs->from, &line[22], ln_call);
  96.     strncpy(tmmhs->bbs, &line[29],ln_call);
  97.     strncpy(tmmhs->date, &line[36], ln_date);
  98.     strncpy(tmmhs->time, &line[43], ln_time);
  99.     fill(tmmhs->bid, ' ', ln_bid);
  100.     if (line[48] isnt ' ') unbl(tmmhs->bid, &line[48], ln_bid);
  101.     tmmhs->ext = line[68] - '0'; 
  102.     if (tmmhs->ext is 1)
  103.     {
  104.       strncpy(tmmhs->flag, &line[70], mmesn);
  105.       for( x=0, c=0; x < mmesn; x++, c++)
  106.         {
  107.         if (tmmhs->flag[x] is ' ') { tmmhs->flag[x] = '0'; c--;}
  108.         tmmhs->flag[x] = (tmmhs->flag[x]-'0');
  109.         }
  110.         tmmhs->count = c;
  111.         y = dodis();
  112.     }
  113.     if (tmmhs->ext is 2)
  114.     {
  115.       strcpy (cmd, &line[70]);
  116.       remnl (cmd);
  117.       strcpy(tmmhs->call[0], cmd);
  118.       t = strchr(&line[68], '\n');
  119.       t++;
  120.       strcpy(cmd, t);
  121.       remnl(cmd);
  122.       strcpy(tmmhs->title, cmd);
  123.     }
  124.     else
  125.     {
  126.       parse();
  127.       strcpy(tmmhs->title, fld[0]);
  128.     }
  129.     write_rec(mail,recno++,(char *)tmmhs);
  130.     close(msgfl);
  131.     if (y) puts("restored");
  132.     xx++;
  133.   }
  134.   mfhs->next    = recno;
  135.   mfhs->first   = 1;
  136.   mfhs->last    = recno-1;
  137.   mfhs->version = mb_version;
  138.   mfhs->free    = 0;
  139.   mfhs->count   = recno-1;
  140.   mfhs->unt_msg = 1;
  141.   fill(mfhs->unu, '\0', mfhsunu);
  142.   fill(mfhs->date, '\0', ln_date);
  143.   fill(mfhs->time, '\0', ln_time);
  144.   x = 1;
  145.   if (count)  x = 1 + atoi(name[count - 1]);
  146.   printf("The next message number will be %u\n", x);
  147.   printf("Enter return if OK or new message number  ");
  148.   gets(cmd);
  149.   if (*cmd)  mfhs->next_msg = atoi(cmd);
  150.   else  mfhs->next_msg = x;
  151.   mfhs->unt_msg = mfhs->next_msg;
  152.   write_rec(mail, 0, (char *)mfhs);
  153.  
  154.   close(mail);
  155. }
  156.  
  157. /*
  158.  *  Parse a command line.
  159.  *
  160.  *  Input:   Line of text in line.
  161.  *  Returns: Fields are placed in cmd.
  162.  *           Fields are pointed to by fld[].
  163.  *           flds is set to the number of fields found.
  164.  *           Each field is null-terminated.
  165.  *           Fields beyond maxflds are ignored.
  166.  */
  167.  
  168. parse()
  169. {
  170.   char skip;
  171.   register short bl;
  172.   register char *in, *out;
  173.  
  174.   for (flds = 0; flds < maxflds;)
  175.     fld[flds++] = nullstr;
  176.  
  177.   in  = &line[86];
  178.   out = cmd;
  179.   bl  = false;
  180.   flds = 0;
  181.   skip = true;
  182.  
  183.   while (*in and (flds < maxflds) and (out < (cmd + linelen - 1)))
  184.   {
  185.     if (*in is '\r') { *in++; continue;}
  186.     if (*in is '\n')  skip = !(skip);
  187.     *out = *in;
  188.     if (bl) {if ((*in <= ' ') and (skip)) { bl = false; *out = '\0'; } out++; }
  189.     else    {if (*in > ' ')  { bl = true; fld[flds++] = out++; }}
  190.     in++;
  191.   }
  192.   *out = '\0';
  193. }
  194.  
  195.  
  196. /*
  197.  *  Remove new line  and carriage returns from end of string.
  198.  */
  199.  
  200. remnl(p)
  201. char *p;
  202. {
  203.   for (; *p; p++)
  204.   {
  205.     if ((*p is '\n') or (*p is '\r')) { *p = '\0'; return; }
  206.   }
  207. }
  208. /*
  209.  *  Read random record.
  210.  */
  211.  
  212. read_rec(fid, rec, buffer)
  213. int fid;
  214. int rec;
  215. char buffer[];
  216. {
  217.   long lseek();
  218.   long offs;
  219.  
  220.   offs = (long)rec * (long)RECSIZE;
  221.   lseek(fid, offs, 0);
  222.   return (read(fid, buffer, RECSIZE) is RECSIZE);
  223. }
  224.  
  225. /*
  226.  *  Write random record.
  227.  */
  228.  
  229. write_rec(fid, rec, buffer)
  230. int fid;
  231. int rec;
  232. char buffer[];
  233. {
  234.   long lseek();
  235.   long offs;
  236.  
  237.   offs = (long)rec * (long)RECSIZE;
  238.   lseek(fid, offs, 0);
  239.   return (write(fid, buffer, RECSIZE) is RECSIZE);
  240. }
  241. /*
  242.  *  Fill some memory with a character.
  243.  */
  244.  
  245. fill(adr, ch, len)
  246. char *adr;
  247. char ch;
  248. int len;
  249. {
  250.   while (len--) *adr++ = ch;
  251. }
  252.  
  253. /*
  254.  *  Create the distribution list, if required.
  255.  */
  256.  
  257. dodis()
  258. {
  259.   register FILE *dfl;
  260.   char c;
  261.   char tmp[linelen];
  262.  
  263.   tmmhs->ext = 0;
  264.   if (*tmmhs->bbs is ' ') return;
  265.   unbl(tmp, tmmhs->bbs, ln_call);
  266.   strcat( tmp, ".DIS");
  267.  
  268.   if ((dfl = fopen( tmp, "r")) is NULL)
  269.      {
  270.      printf("restored without %s (file is not found)\n", tmp);
  271.      return false;
  272.      }
  273.   tmmhs->ext = 0x01;
  274.  
  275.   for (c = 0; (c <tmmhs->count) and (fgets( tmp, linelen, dfl) isnt NULL);
  276.     c++)
  277.     {
  278.     strupr(tmp);
  279.     pcall(tmmhs->call[c], tmp);
  280.     }
  281.   fclose(dfl);
  282.   return true;
  283. }
  284. /*
  285.  *  Copy LJSF string to C string.
  286.  */
  287.  
  288. unbl(to, from, size)
  289. char *to, *from;
  290. int size;
  291. {
  292.   while (size--)
  293.   {
  294.     if (*from <= ' ') { *to = '\0'; return; }
  295.     *to++ = *from++;
  296.   }
  297.   *to = '\0';
  298. }
  299. /*
  300.  *  Parse a callsign.
  301.  *  Blank pad the output field, remove trailing ssid from call.
  302.  *  Return the SSID.
  303. */
  304.  
  305. pcall(c, p)
  306. char *c;
  307. char *p;
  308. {
  309.   register short i;
  310.  
  311. /*
  312.  *  Blank fill the target buffer.
  313.  */
  314.  
  315.   fill (c, ' ', ln_call);
  316.  
  317. /*
  318.  *  Ignore leading spaces.
  319.  */
  320.  
  321.   while (*p and (*p is ' ')) p++;
  322.  
  323. /*
  324.  *  Copy the call from the string into the call buffer.
  325.  */
  326.  
  327.   for (i = ln_call; i and *p; i--)
  328.   {
  329.     if (*p <= ' ') return 0;
  330.     if (*p is '-') return atoi(++p);
  331.     *c++ = *p++;
  332.   }
  333.   if (*p++ is '-') return atoi(p); else return 0;
  334. }
  335.  
  336. /*
  337.  *  Read the file names from the current directory and store
  338.  *  the results in an array but exclude any name that is not
  339.  *  made up of all numbers.
  340.  *  Sort the resulting list in ascending order.
  341.  */
  342.  
  343. dir_list()
  344. {
  345.   int x, y;
  346.   char *file_name = "*.";
  347.  
  348.   printf("Reading directory - ");
  349.   bdos(0x1a, (unsigned)pp, 0);
  350.   if(18==bdos(0x4e, (unsigned)file_name, 0))
  351.     {
  352.       printf("No message files are found\n");
  353.       return;
  354.     }
  355.  
  356.   if (num( &pp[30])) strcpy (name[count++], &pp[30]);
  357.  
  358.   for(;;)
  359.   {
  360.     if(18==bdos(0x4f, 0, 0)) break;
  361.     if (num( &pp[30])) strcpy (name[count++], &pp[30]);
  362.   }
  363.   printf("There are %d message files\n", count);
  364.   if (count)
  365.     {
  366.       printf("***Sorting***\n");
  367.       qsort (name, count, 13, cmp);
  368.       for(x=0, y=1; x< count; x++, y++) 
  369.       {
  370.         printf("%6s", name[x]);
  371.         if(y is 10) { printf("\n"); y=0; }
  372.       }
  373.       printf("\n");
  374.     }
  375. }
  376.  
  377. /*
  378.  *  Compare two strings converted to integers.
  379.  */
  380.  
  381. int cmp(n1,n2)
  382. char *n1;
  383. char *n2;
  384. {
  385.    return (atoi(n1) - atoi(n2));
  386.  
  387. }
  388.  
  389. /*
  390.  *  Is the string a number?
  391.  */
  392.  
  393. num(p)
  394. char *p;
  395. {
  396.   for (; *p; p++) if (!isdigit(*p)) return false;
  397.   return true;
  398. }
  399.